define(function(require, exports, module){
	var ajax = require("resource/js/component/ajax.js");
	var urlTool = require("resource/js/component/urlTool.js");
	var painNodeForm = require("oa/act/mobile/js/paintNodeFormMoblie.js");
	var actcont = require("oa/act/js/actcont.js");
	var submit = require("oa/act/mobile/js/submitMobile.js");
	
	
	var processColor={"审批中":"#ff851a","同意":"#61b361", "提交":"#61b361","转交":"#5f97f6","拒绝":"#ff0000","终止":"#ff0000","驳回":"#ff0000","":"#b4b5b8"};
	var colors = ['#229af9', '#e91e63', '#ffc107', '#8bc34a', '#3f51b5', '#00bcd4'];
	var businessId='';
	exports.initPage=function(){
		
		if(window.js&&window.js.isGoback){
		    window.js.isGoback("true");
		}
		businessId = urlTool.getParam("bussId");
		var url = urlTool.getParam("url");
		var status = urlTool.getParam("status");
		var title =  decodeURI(urlTool.getParam("title"));
		var processName = decodeURI(urlTool.getParam("processName"));
		window.document.title = urlTool.ToGB2312(title);
		if(title === '我的审核'){		
			$('#withdrawBtn').remove();
			/*$('#filedBtn').remove();*/
			$('#deleteBtn').remove();
			$('#btns').hide();		
		}else{
			if(status == '4'){
				$('#withdrawBtn').remove();
			}
			if(status != '0'){
				$('#withdrawBtn').remove();
				$('#deleteBtn').remove();
			}
/*			if(status == '0'){
				$('#filedBtn').remove();
			}	*/		
		}
		if(url!=null&&url!=""){
			debugger
			ajax.init().success(function(code,msg,data){
				if(code == 0){
					data = data.data;
					if(data){
						var mainView = data[0];	
						if(mainView.hasOwnProperty("main") && mainView.main.length>0){
							var mainHtml = '<div class="result-box padding10" id="view_main" style="margin-top:10px">'
								+'<div class="result-title"><span>' + mainView.username + '的'+ (processName||'') + '申请</span></div>'
								+'<div class="prompt"><span>申请时间：' + mainView.dealtime + '</span></div>'
								+'<hr/>'
								+'<ul id="tab_0"></ul></div>';	
							var detail = detailInfo(mainView.main[0])+'<hr/>';
							if(mainView.hasOwnProperty("subs") && mainView.subs.length>0){
								detail += detailInfo(mainView.subs[0])+'<hr/>'
							}
							$('#resultForm').append(mainHtml);
							$('#tab_0').append(detail);
								
						}
						$("#resultForm").append('<div class="result-box result-process padding10"><div id="view_process"><div class="line"></div></div></div>');
						var index = 1;
						for(var k in data){
							var tabId = 'tab_'+index;
						var itemHtml = '<div class="result-process-item">'
								+'<div class="process-user-photo" >'
									+'<span>'+data[k].username.substring(0, 1)+'</span>'
								+'</div>' 
							+'<div class="process-deal-info" id="'+tabId+'">'
							+'<span>' + data[k].username + '</span>'
							+'<span style="color:'+processColor[data[k].action]+'">'+(data[k].action||'')+'</span>'
							+'<span class="prompt" style="float:right;">' + (data[k].dealtime||'') + '</span>'
							+'</div></div>';
							$("#view_process").append(itemHtml);
							if(!data[k].main || data[k].main.length==0){
								if(data[k].subs&&data[k].subs.length>0){
									$.each(data[k].subs,function(i,subs){
										var html = detailInfo(subs);
										$(html).appendTo($('#'+tabId));
										$('#'+tabId).find('ul').addClass('process-deal-advice');
									})
								}
							}
							if(data[k].action=="终止"|| data[k].action=="拒绝"){
								break;
							}
							index++;
						}					
					}
				}
			}).post(BASEPATH+"/oa/act/actProgressTable/queryOverBussInfo.htm",{'path':url});
		}else{
			var key=urlTool.getParam("key");
			var bussId=urlTool.getParam("bussId");
			var action="";
			var dealTime="";
			ajax.init().success(function(code,msg,datas){
				if(code == 0){
					if(datas){
						if(datas.view&&datas.view.length>0){						
							var mainView = datas.view[0];						
							var mainHtml = '<div class="result-box padding10" id="view_main" style="margin-top:10px">'
								+'<div class="result-title"><span>' + mainView.username + '的'+ processName + '申请</span></div>'
								+'<div class="prompt"><span>申请时间：' + mainView.dealtime + '</span></div>'
								+'<hr/>'
								+'<ul class="" id="tab_0"></ul></div>';
							$('#resultForm').append(mainHtml);
							painNodeForm.load_view(key, mainView.processnodeid, datas.busenessId, mainView.taskid, 'tab_0');
							
							$("#resultForm").append('<div class="result-box result-process padding10"><div id="view_process"><div class="line"></div></div></div>');
							var itemHtml = "";
							var index = 1;	
							$.each(datas.view, function(i, view) {
								itemHtml = ""
								var tabId = "tab_" + index;
								itemHtml += '<div class="result-process-item">'
										+'<div class="process-user-photo" >'
											+'<span>'+view.username.substring(0, 1)+'</span>'
										+'</div>' 
									+'<div class="process-deal-info">'
									+'<span>' + view.username + '</span>'
									+'<span style="color:'+processColor[view.action]+'">'+(view.action||'')+'</span>'
									+'<span class="prompt" style="float:right;">' + (view.dealtime||'') + '</span>'
									+'<ul class="process-deal-advice" id="' + tabId + '"></ul></div></div>';
								$("#view_process").append(itemHtml);
								//绘制明细信息
								if(view.taskid && i > 0 && view.action != '转交'){
									painNodeForm.load_view(key, view.processnodeid, datas.busenessId, view.taskid, tabId);
								}						

								if(view.action == '终止'||view.action == '拒绝'){
									return false;
								}
								index++;
							});
						}
					}
				}else{
					if(code==11&&window.js&&window.js.logout){
						window.js.logout();
					}
					$(".error").text(msg).show().delay(1000).hide(0);
				}
			}).post(BASEPATH+"/oa/act/activitiMain/getMobileProcessViewPage.htm",{key:key,bussId:bussId});
		}
		
		setTimeout(function(){
			$('<a id="printBtn" class="printBtn"></a>').appendTo($('.result-title'));
			$('.line').height($("#view_process").height()-$("#view_process").find('.result-process-item:last-child').height())
			addBtnEvent();
			addFileEvent();
			loadCCUsers();
			}, 200);
	};
	var detailInfo = function(info){
		var htmladd ="";
		var colhtml="";
		if(info && info.data&&info.data.length>0){
			$.each(info.data,function(i,data){
				 colhtml="<ul class='list marginBottom10'>";
				 if(info.tab.ismore==0){
					 colhtml+="<div class='sub-title'>"+info.tab.tablename+"(<span>"+(i+1)+"</span>)</div>";
				 }
				$.each(info.cols,function(i,cols){
					show = data?true:false;
					if(cols.showtype!=actcont.showType.FORM_SHOWTYPE_HIDDEN){
						var  coname = cols.colname; 
						if(cols.showtype==actcont.showType.FORM_SHOWTYPE_FILE){
							colhtml += "<li"+(show?"":" style='display:none'")+"><span class='col-name'>"+cols.colnamech+"：</span>"
							if(data!=null&&data[coname]!=undefined&&data[coname]!="" && info.dataaddon){
								var files = info.dataaddon[cols.colname];
								if(files&&files.length>0){
									var fileDownload='<div class="fileList">';
									for(var i=0;i<files.length;i++){
										var file = files[i];
										var fileUrl = file.fileUrl;
										var fileName = file.fileName+"."+file.fileExt;
										var size = getFileSize(file.fileSize);
										var icon = getFileIcon(file.fileExt);
										//fileDownload +="<span class='col-val'><a href='javascript:window.open (\""+BASEPATH+fileUrl+"\")'>"+fileName+"</a></span>&nbsp;&nbsp;";
								   		fileDownload += '<div class="fileSet" data-url='+fileUrl+'>'
										   					+ '<div class="'+icon+'">'
										   						+'<p>'+fileName+'</p>'
										   						+'<p class="prompt">'+size+'</p>'
									   						+'</div></div>';
									}
									colhtml += fileDownload;
								}
							}
							colhtml += "</li>";
						}else{
							colhtml += "<li "+(show?"":" style='display:none'")+"><span class='col-name'>"+cols.colnamech+"：</span><span class='col-val'>"+trim((data!=null&&data[coname]!=null)?data[coname]:"")+"</span></li>";
						}
					}
				})
				colhtml+="</ul>";
				htmladd+=colhtml;
			})
		}
		return htmladd;
	};
	
	function addFileEvent(){
		$(".fileList .fileSet").bind('click',function(){
			var url = $(this).data('url');
			url&&window.open (BASEPATH+url);
		});
	}
	function getFileSize (size){
		var result = size < 1024 ? size.toFixed(2)+'B' 
   				: size < (1024*1024) ? (size/1024).toFixed(2)+'KB'
				:(size/(1024*1024)).toFixed(2)+'MB';
   		return result;
	}
	function getFileIcon(ext){
		var iconClass='';
		if(/^(gif|png|jpg|jpeg|bmp)$/i.test(ext)){
			iconClass = 'pictureIcon';
		}else if(/^(mp3|mp4|avi|dat|mkv|flv)$/i.test(ext)){
			iconClass = 'mediaIcon';
		}else{
			iconClass = 'fileIcon';
		}
		return iconClass;
	}
	function trim(res){
		if(res!=undefined && res!=null && res!=""){
			return res;
		}else{
			return "";
		}
	};

	function loadCCUsers(){
		if(businessId){
			require.async ('resource/js/component/ajax.js',function(ajax){
				ajax.init().success(function(code,msg,datas){
					if(datas!=null&&datas.length>0){
						var html="";
						$.each(datas,function(i,val){
						   var text = val.userName.substring(0,1);
						   var col_index = i%colors.length;
						   html += "<div class='approvers-item'>"
										+"<div  class='approvers-item-person' style='background-color: "+colors[col_index]+"'>"
											+"<span class='name-first'>"+text+"</span>"
										+"</div>"
										+"<div class='personName'>"+val.userName+"</div>"
								  +"</div>";
						});
						$("#copyUser").html(html);
						$("#copyUsers").show();
					}else{
						$("#copyUsers").hide();
					}
				}).post(BASEPATH+"/oa/actCC/queryCCUsers.htm",{businessId:businessId});
			});	
		}			
	}
	function disableBtn(){
		$('#btns').children().addClass('l-btn-disabled');
	}
	function enableBtn(){
		$('#btns').children().removeClass('l-btn-disabled');
	}
	var addBtnEvent = function(){
		if (typeof($("#printBtn")) != 'undefined' && $("#printBtn").length > 0) {
			$("#printBtn").unbind("click");
			$("#printBtn").bind("click", function() {
		    	$(".Mask").show();
		    	$(this).attr("disabled", true);
				ajax.init().success(function(code,msg,data){
					if(code==0){
						var param=data.filePath+data.fileId+"."+data.fileExt;
						$("#addressTest").text(msg);
				    	$(".alertDelete").show();
				    	$("#apply").on("click",function(){
					    	$(".Mask").hide();
					    	$(".alertDelete").hide();
				    		window.location.href=BASEPATH+"/download/file/"+param;
							$(".error").text("下载成功！").show().delay(1000).hide(0);
							$(this).attr("disabled", false)
				    	});
				    	
				    	$(".Mask,#cancel").click(function(){
				    		$(".Mask").hide();
				    		$(".alertDelete").hide();
				    		$(this).attr("disabled", false)
				    	});
					}
				}).error(function(code, msg, data) {
					setTimeout(function() {
						$(this).attr("disabled", false);
						$(".Mask").hide();
					}, 1000);
					$(".error").text("打印失败！").show().delay(2000).hide(0);
				}).post(BASEPATH+"/oa/act/print/printprocess.htm",{"businessId":businessId})
			})
		}
		if (typeof($("#withdrawBtn")) != 'undefined' && $("#withdrawBtn").length > 0) {
			$("#withdrawBtn").unbind("click");
			$("#withdrawBtn").bind("click", function() {
				if($(this).hasClass("l-btn-disabled")){
					return;
				}
				disableBtn();
				ajax.init().success(function(code,msg,data){
					if(code==0){					
						$(".error").text("撤回成功").show().delay(1000).hide(0);
						setTimeout(function() {
							submit.goBack();
						}, 1000);
					}
				}).error(function(code, msg, data) {
					enableBtn();
					$(".error").text("撤回失败！").show().delay(2000).hide(0);				
				}).post(BASEPATH+"/oa/act/generalOperate/withdraw.htm",{"businessId":businessId})
			})
		}
		if (typeof($("#deleteBtn")) != 'undefined' && $("#deleteBtn").length > 0) {
			$("#deleteBtn").unbind("click");
			$("#deleteBtn").bind("click", function() {
				if($(this).hasClass("l-btn-disabled")){
					return;
				}
				disableBtn();
				ajax.init().success(function(code,msg,data){
					if(code==0){
						$(".error").text("删除成功").show().delay(1000).hide(0);
						setTimeout(function() {
							submit.goBack();
						}, 1000);
					}
				}).error(function(code, msg, data) {
					enableBtn();
					$(".error").text("删除失败！").show().delay(2000).hide(0);
				}).post(BASEPATH+"/oa/act/generalOperate/delete.htm",{"businessId":businessId})
			})
		}
		if (typeof($("#filedBtn")) != 'undefined' && $("#filedBtn").length > 0) {
			if($(this).hasClass("l-btn-disabled")){
				return;
			}
			$("#filedBtn").unbind("click");
			$("#filedBtn").bind("click", function() {
				disableBtn()
				ajax.init().success(function(code,msg,data){
					if(code==0){
						$(".error").text("归档成功").show().delay(1000).hide(0);
						setTimeout(function() {
							submit.goBack();
						}, 1000);
					}
				}).error(function(code, msg, data) {
					enableBtn();
					$(".error").text("归档失败！").show().delay(2000).hide(0);
				}).post(BASEPATH+"/oa/act/Filed/Filed.htm",{"businessId":businessId})
			})
		}
	}
});


